<?php

namespace app\common\model\haipi;

use think\facade\Db;
use think\Model;

/**
 * Discuz
 */
class Discuz extends Model
{
    // 表名
    protected $name = 'haipi_discuz';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';


    const SOURCE_TYPE_SELF = 'self';

    const STATUS_NORMAL = 1;
    const STATUS_DISABLE = 0;

    const AUDIT_STATUS_PASS = 1; // 审核通过
    const AUDIT_STATUS_DOING = 0; //  审核中
    const AUDIT_STATUS_FAIL = 2; // 审核不通过


    public function getImagesAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setImagesAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getAreasAttr($value, $data): array
    {
        
       $areas = [$data['province_code'],$data['city_code'],$data['district_code']];

        $value = Db::name('area')->whereIn('code',$areas)->column('id');
      
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setAreasAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getAreasTextAttr($value, $row): string
    {
    
        $row['areas'] = [$row['province_code'], $row['city_code'], $row['district_code']] ??'';

        if ($row['areas'] === '' || $row['areas'] === null) return '';
        $cityNames = Db::name('area')->whereIn('code', $row['areas'])->column('name');
        return $cityNames ? implode(',', $cityNames) : '';
    }


    public function getContentAttr($value): string
    {
        return !$value ? '' : htmlspecialchars_decode($value);
    }

    public function getIsHomeAttr( $value, $row){
        $exist = HomeRecommendation::where('type', 'discuz')
        ->where('source_id', $row['id'])
        ->field('id')
        ->count() > 0;
        return $exist ? 1 : 0;
    }

    protected static function onAfterInsert($model): void
    {
        if (is_null($model->weigh)) {
            $pk = $model->getPk();
               // 获取最大的权重值
               $weigh = $model->order('weigh', 'desc')->limit(1)->value('weigh');
                    
               $model->where($pk, $model[$pk])->update(['weigh' => (int)$weigh + 1]);
        }
    }

    public static  function onAfterWrite($model)
    {
        $data = $model->getData();
        if(!isset($data['is_home'])) {
            return ;
        }
        
        if($data['is_home'] == 1) {
            $exist = HomeRecommendation::where('type', 'discuz')
            ->where('source_id', $data['id'])
            ->field('id')
            ->count() > 0;
            if(!$exist) {
                HomeRecommendation::create([
                    'type' => 'discuz',
                    'source_id' => $data['id']
                ]);
            }
        }else{
            HomeRecommendation::where('type', 'discuz')
                ->where('source_id', $data['id'])
                ->delete();
        }
    }

    public function getContactAttr($value, $row){
        if($value){
            return $value;
        }
        $contacts = [];
        if($row['wx']){
            $contacts[] = '微信:'.$row['wx'];
        }
        if($row['qq']){
            $contacts[] = 'qq:'.$row['wx'];
        }
        if($row['mobile']){
            $contacts[] = '手机:'.$row['mobile'];
        }
        if($row['yuni']){
            $contacts[] = '与你:'.$row['yuni'];
        }
        return implode(',', $contacts);
    }
}